//
//  UserEngineNav.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/6/10.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class UserEngineNav: NSView {
    
    var uBtns:[MSImageView] = []
    var kWidth:CGFloat = 15
    var kHeight:CGFloat = 12
    var space:CGFloat = 15
    
    var indexClick:ResultInt?
    var selectIndex:Int = 1 {
        didSet{
            self.setSelectView(index: self.selectIndex)
        }
    }
    
    var list:([String],[String]) = ([],[])
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    convenience init(data:([String],[String]),index:Int) {
        self.init()
        self.list = data
        self.selectIndex = index
        self.setupUI()
        self.loadData()
    }
    
    func loadData() {
        self.setSelectView(index: self.selectIndex)
    }
    
    func setSelectView(index:Int) {
        self.backInit()
        self.uBtns[index].image = NSImage.image(name:self.list.1[index])
    }
    
    func backInit() {
        for (inx,btn) in self.uBtns.enumerated() {
            btn.image = NSImage.image(name:self.list.0[inx])
        }
    }
    
    func setupUI() {
        self.addSubview(menuView)
        menuView.snp.makeConstraints { (make) in
            make.width.equalTo((kWidth+space)*CGFloat(self.list.0.count)-space)
            make.left.equalTo(20)
            make.height.equalTo(kHeight)
            make.centerY.equalToSuperview()
        }
        
        self.uBtns = self.initMenu(inView: self.menuView)
    }
    
    lazy var menuView: NSView = {
        let view = NSView()
        return view
    }()
    
    func initMenu(inView:NSView) -> [MSImageView] {
        var btns:[MSImageView] = []
        for (inx,image) in list.0.enumerated() {
            let btn = MSImageView.init(image: NSImage.image(name:image)!)
            btn.tag = inx
            btn.tap = { view in
                self.btnAction(senber: view as! MSImageView)
            }
            inView.addSubview(btn)
            btns.append(btn)
            btn.snp.makeConstraints { (make) in
                make.width.equalTo(kWidth)
                make.height.equalTo(kHeight)
                make.centerY.equalToSuperview()
                if inx == 0 {
                    make.left.equalTo(0)
                }else {
                    make.left.equalTo(btns[inx-1].snp.right).offset(space)
                }
            }
        }
        return btns
    }
    
}

extension UserEngineNav {
    @objc func btnAction(senber:MSImageView) {
        self.selectIndex = senber.tag
        if self.indexClick != nil {
            self.indexClick!(senber.tag)
        }
    }
}
